package com.v1.ourdiary.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.MultiValueMap;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import weibo4j.model.User;
import weibo4j.model.UserTrend;
import weibo4j.model.WeiboException;

import com.v1.model.Diary;
import com.v1.model.Pair;
import com.v1.util.Toolkit;
import com.v1.web.action.DefaultAction;
import com.v1.weibo4j.Weibo4jLO;

public class OurDiaryAction extends DefaultAction {
	protected static final ActionForward ERROR = new ActionForward("", "/ourdiary/error.html", true, "");

	protected ActionForward getErrorForward() {
		return ERROR;
	}

	@Override
	protected ActionForward before(HttpServletRequest request, HttpServletResponse response) {
		ActionForward before = super.before(request, response);
		if (before != null) {
			return before;
		}
		Weibo4jLO agent = (Weibo4jLO) getBean("Weibo4jLO");
		if (!agent.isLogin()) {
			goLogin(request);
			return new ActionForward("", "/ourdiary/index.html", true, "");
		}
		return null;
	}

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return trendsAndFriends(mapping, form, request, response);
	}

	public ActionForward line(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Weibo4jLO agent = (Weibo4jLO) getBean("Weibo4jLO");
		List<UserTrend> trends = agent.getTrends();
		setAttribute(request, "trends", trends);

		String str = getString(request, "trend");
		String trend = Toolkit.decodeZH(str);
		if (StringUtils.isEmpty(trend)) {
			if (CollectionUtils.isEmpty(trends)) {
				return mapping.findForward("ourdiary");
			}
			trend = trends.get(0).getHotword();
		}
		setAttribute(request, "trend", trend);

		List<User> paticipators = agent.getTrendPaticipator(trend);
		setAttribute(request, "paticipators", paticipators);

		List<Diary> list = agent.getTrendline(trend);
		setAttribute(request, "line", list);

		// long currentTimeMillis = System.currentTimeMillis();
		// for (Diary diary : list) {
		// long time = diary.getCreatedAt().getTime();
		// long interval = currentTimeMillis - time;
		// }

		return mapping.findForward("ourdiary");
	}

	public ActionForward publish(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Weibo4jLO agent = (Weibo4jLO) getBean("Weibo4jLO");
		String statuses = getString(request, "statuses");
		agent.UpdateStatus(statuses);
		return unspecified(mapping, form, request, response);
	}

	public ActionForward followTrend(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Weibo4jLO agent = (Weibo4jLO) getBean("Weibo4jLO");
		String trends[] = getStrings(request, "trend");
		try {
			for (String trend : trends) {
				agent.trendsFollow(trend);
			}
			return mapping.findForward("line");
		} catch (WeiboException e) {
			String msg = ((WeiboException) e).getError();
			setAttribute(request, "msg", msg);
			return mapping.findForward("trendsAndFriends");
		}
	}

	public ActionForward trendsAndFriends(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Weibo4jLO agent = (Weibo4jLO) getBean("Weibo4jLO");
		List<UserTrend> trends = agent.getTrends();
		StringBuilder sb = new StringBuilder();
		for (UserTrend userTrend : trends) {
			sb.append("#");
			sb.append(userTrend.getHotword());
		}
		setAttribute(request, "alltrends", sb.toString());
		List<Pair<User, List<UserTrend>>> allFriendTrends = agent.getAllFriendTrends();
		setAttribute(request, "allFriendTrends", allFriendTrends);
		MultiValueMap trendPaticipator = agent.getTrendPaticipator();
		setAttribute(request, "trendPaticipator", trendPaticipator);
		return mapping.findForward("trendsAndFriends");
	}

	public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Weibo4jLO agent = (Weibo4jLO) getBean("Weibo4jLO");
		agent.refresh();
		String redirect = getString(request, "redirect");
		return new ActionForward("", "/ourdiary/" + redirect + ".html", true, "");
	}

}